<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Gregwar\Captcha\CaptchaBuilder;
use Session;
use DB;
use Crypt;
use Mail;

class LoginController extends Controller
{
    
    public function index()
   	{
   		// return '登录';
   		return view('home.login.login');
   	}

   	public function dologin(Request $request)
   	{
   		//dd($request -> all());
 		$data = $request -> all();
 		$request -> flash();
   		$scode = session('milkcaptcha');
   		
   		
   		
   		//dd($data);

   		//dd(empty($data['email']));
   		if(empty($data['email']))
		{
			return back() -> with(['em' => '邮箱不能为空']);
		}else if(empty($data['password']))
		{
			return back() -> with(['passwd' => '密码不能为空']);
		}else if(empty($data['code']))
		{
			return back() -> with(['ce' => '验证码不能为空']);
		}



		if($scode != $data['code'])
   		{
   			return back() -> with(['ce' => '您输入验证码错误']);
   		}
		$res = DB::table('users') -> where('email',$data['email'] ) -> first();
    	
    	if(!$res){
    		
    		return back() -> with(['em' => '邮箱不存在']);
    	
    	}
    	//dd($res);

    	// 判断密码
    	$oldpassword = DB::table('users') ->where('email',$data['email']) -> first();
    	$old = $oldpassword -> password;
    	// dd($old);
    	// dd($oldpassword);
    	$olds = Crypt::decrypt($old);
    	//dd($olds); 
    	if(!($olds == $data['password']))
    	{
    		return back() -> with(['passwd' => '密码不正确']);
    	}

    	

    	$id = $res -> id;
    	 // dd($id);
        $name = $res ->name;
        session(['id' => $id]);
        session(['name' => $name]);
    	session(['token' => $name]);
        return redirect('/');
 	  }
    public function reset()
      {
        
          return view('home.login.reset');
      }


      public function doreset(Request $request)
        {
        

        $res = DB::table('users') -> where('email', $request -> input('email')) -> first();
 
          if(!$res)
         {
            return back() -> with(['em' => '不存在这个邮箱，请输入正确的邮箱']);
         }


        $flag = Mail::send('home.login.send',['res' => $res], function ($message) use ($request){
             $message -> subject('这是一封来自于小米的重置密码的邮件');
             $message -> to($request -> input('email'));
         });
        if(!$flag)
        {
            return view('home.login.info',['info' => '邮件发送失败']);
        }
        return view('home.login.info',['info' => '已发送，请前往邮箱查收']);
        
    }


     // newPass
    public function newPass($id,$token)
    {
        $data = DB::table('users')
            -> where('id', $id)
            // -> where('token', $token)
            -> first();

        if(!$data)
        {
            return view('home.login.info',['info' => '不是一个合法的来源，请从邮箱过来']);
        }else
        {

            return view('home.login.reupdate',['data' => $data]);
        }

    }


     public function update(Request $request)
    {

        $data = $request -> only('password');
        // dd($data);
        if(empty($request -> input('repassword')))
        {
            return back() -> with(['repasswd' => '确认密码不能为空']);
        }else if(empty($data['password']))
        {
            return back() -> with(['passwd' => '密码不能为空']);
        }else if(empty($request -> input('code')))
        {
            return back() -> with(['ce' => '验证码不能为空']);
        }

        if($request -> input('password') != $request -> input('repassword'))
        {
            return back() -> with(['info' => '两次密码不一样']);
        }


        $res = DB::table('users')
            -> where('id', $request -> input('id'))
            -> update($data);

        if($res)
        {
            return view('home.login.info',['info' => '修改密码成功']);
        }else
        {
            return view('home.login.info',['info' => '修改密码失败']);
        }
    }

}
